package com.yangcangclub.api.v1.trade.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.yangcangclub.commons.annotation.*;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.commons.result.ResultData;
import com.yangcangclub.commons.utils.StringUtil;
import com.yangcangclub.pay.utils.XmlUtil;
import com.yangcangclub.trade.bean.TradeVo;
import com.yangcangclub.trade.bean.UserAddress;
import com.yangcangclub.trade.service.TradeService;
import com.yangcangclub.trade.service.UserAddressService;
import com.yangcangclub.user.component.UserComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

/**
 * 用户地址管理
 */
@Controller
@RequestMapping("/yangcangclub/v1/user/*")
public class UserAddressController {
    private static Logger logger = LoggerFactory.getLogger(UserAddressController.class);

    @Autowired
    UserAddressService userAddressService;

    /**保存用户地址信息
     * @param user
     * @param province
     * @param city
     * @param county
     * @param contactName
     * @param contactMobile
     * @param contactAddress
     * @param isDefault
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/save", method = { RequestMethod.POST })
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData save(@GetUser User user, String province,
                                  String city, String county, String contactName,
                                  String contactMobile, String contactAddress,
                                    String address,
                                  @RequestParam(required=false,defaultValue = "0")int isDefault){
        ResultData resultData ;
        try {
            //判断
            //判断用户总有效地址数
            if(userAddressService.getUserAddressListCount(user.getId())>8){
                throw new ApolloException(MessageEnum.TOO_MANY_ADDRESSES);
            }
            //业务
            UserAddress userAddress = new UserAddress();
            userAddress.setIsDefault(isDefault);
            userAddress.setProvince(province);
            userAddress.setCity(city);
            userAddress.setCounty(county);
            userAddress.setContactName(contactName);
            userAddress.setContactMobile(contactMobile);
            userAddress.setContactAddress(contactAddress);
            userAddress.setAddress(address);
            userAddress.setUserId(user.getId());
            userAddress.setIsDelete(0);
            userAddress.setCreateDate(new Date());
            boolean b = userAddressService.saveUserAddress(userAddress);
            if(b){
               return resultData = new ResultData(MessageEnum.SUCCESS);
            }
            return resultData = new ResultData(MessageEnum.FAILURE);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /**获取地址信息
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/showList")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData showList(@GetUser User user){
        ResultData resultData ;
        try {
            List<UserAddress> list = userAddressService.getUserAddressList(user.getId());
            resultData = new ResultData(MessageEnum.SUCCESS,list);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /**获取地址详细信息
     * @param user
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/details")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData details(@GetUser User user,int id){
        ResultData resultData ;
        try {
            UserAddress userAddress = userAddressService.getUserAddressDetails(id);
            resultData = new ResultData(MessageEnum.SUCCESS,userAddress);
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /** 获取默认地址信息
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/getDefaultAddress")
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData getDefaultAddress(@GetUser User user){
        ResultData resultData ;
        try {
            UserAddress userAddress = userAddressService.getDefaultUserAddress(user.getId());
            if(userAddress == null){
                resultData = new ResultData(MessageEnum.FAILURE);
            }else{
                resultData = new ResultData(MessageEnum.SUCCESS,userAddress);
            }
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }
    /**删除用户地址
     * @param user
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/delete" , method = { RequestMethod.POST })
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData delete(@GetUser User user,int id){
        ResultData resultData ;
        try {
            boolean b = userAddressService.deleteUserAddressById(id);
            if( b ){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else{
                resultData = new ResultData(MessageEnum.FAILURE);
            }
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }

    /** 将某个地址设置为默认
     * @param user
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/default" , method = { RequestMethod.POST })
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData setDefault(@GetUser User user,int id){
        ResultData resultData ;
        try {
            boolean b = userAddressService.updateUserAddressDefault(user.getId(),id);
            if( b ){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else{
                resultData = new ResultData(MessageEnum.FAILURE);
            }
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }
    /** 修改地址信息
     * @param user
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/address/update" , method = { RequestMethod.POST })
    @ApolloSecurity(auth = AuthEnum.USER,limit = 50,time = TimeEnum.MINUTE)
    public ResultData update(@GetUser User user, String province,
                             String city, String county, String contactName,
                             String contactMobile, String contactAddress,
                             String address,
                             @RequestParam(required=false,defaultValue = "0")int isDefault,int id){
        ResultData resultData ;
        try {
            UserAddress userAddress = userAddressService.getUserAddressDetails(id);
            if(userAddress == null){
                return resultData = new ResultData(MessageEnum.FAILURE);
            }
            userAddress.setProvince(province);
            userAddress.setCity(city);
            userAddress.setCounty(county);
            userAddress.setContactName(contactName);
            userAddress.setContactMobile(contactMobile);
            userAddress.setContactAddress(contactAddress);
            userAddress.setAddress(address);
            userAddress.setIsDefault(isDefault);
            boolean b = userAddressService.updateUserAddress(userAddress);
            if( b ){
                resultData = new ResultData(MessageEnum.SUCCESS);
            }else{
                resultData = new ResultData(MessageEnum.FAILURE);
            }
        }  catch (ApolloException apoEx) {
            resultData=new ResultData(apoEx);
        }catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultData=new ResultData(e);
        }
        return resultData;
    }
}
